home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / cp-type2.c < prev    next >
C/C++ Source or Header  |  1993-12-09  |  53KB  |  1,763 lines

  1. /* Report error messages, build initializers, and perform
  2.    some front-end optimizations for C++ compiler.
  3.    Copyright (C) 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  4.    Hacked by Michael Tiemann (tiemann@cygnus.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* This file is part of the C++ front end.
  24.    It contains routines to build C++ expressions given their operands,
  25.    including computing the types of the result, C and C++ specific error
  26.    checks, and some optimization.
  27.  
  28.    There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
  29.    and to process initializations in declarations (since they work
  30.    like a strange sort of assignment).  */
  31.  
  32. #include "config.h"
  33. #include <stdio.h>
  34. #include "tree.h"
  35. #include "cp-tree.h"
  36. #include "flags.h"
  37.  
  38. static tree process_init_constructor ();
  39. extern void pedwarn (), error ();
  40.  
  41. extern int errorcount;
  42. extern int sorrycount;
  43.  
  44. /* Print an error message stemming from an attempt to use
  45.    BASETYPE as a base class for TYPE.  */
  46. tree
  47. error_not_base_type (basetype, type)
  48.      tree basetype, type;
  49. {
  50.   if (TREE_CODE (basetype) == FUNCTION_DECL)
  51.     basetype = DECL_CLASS_CONTEXT (basetype);
  52.   cp_error ("type `%T' is not a base type for type `%T'", basetype, type);
  53.   return error_mark_node;
  54. }
  55.  
  56. tree
  57. binfo_or_else (parent_or_type, type)
  58.      tree parent_or_type, type;
  59. {
  60.   tree binfo;
  61.   if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type))
  62.     return parent_or_type;
  63.   if (binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0))
  64.     {
  65.       if (binfo == error_mark_node)
  66.     return NULL_TREE;
  67.       return binfo;
  68.     }
  69.   error_not_base_type (parent_or_type, type);
  70.   return NULL_TREE;
  71. }
  72.  
  73. /* Print an error message stemming from an invalid use of an
  74.    aggregate type.
  75.  
  76.    TYPE is the type or binfo which draws the error.
  77.    MSG is the message to print.
  78.    ARG is an optional argument which may provide more information.  */
  79. void
  80. error_with_aggr_type (type, msg, arg)
  81.      tree type;
  82.      char *msg;
  83.      HOST_WIDE_INT arg;
  84. {
  85.   tree name;
  86.  
  87.   if (TREE_CODE (type) == TREE_VEC)
  88.     type = BINFO_TYPE (type);
  89.  
  90.   name = TYPE_NAME (type);
  91.   if (TREE_CODE (name) == TYPE_DECL)
  92.     name = DECL_NAME (name);
  93.   error (msg, IDENTIFIER_POINTER (name), arg);
  94. }
  95.  
  96. /* According to ARM $7.1.6, "A `const' object may be initialized, but its
  97.    value may not be changed thereafter.  Thus, we emit hard errors for these,
  98.    rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
  99.    example, conversions to references.)  */
  100. void
  101. readonly_error (arg, string, soft)
  102.      tree arg;
  103.      char *string;
  104.      int soft;
  105. {
  106.   char *fmt;
  107.   void (*fn)();
  108.  
  109.   if (soft)
  110.     fn = pedwarn;
  111.   else
  112.     fn = error;
  113.  
  114.   if (TREE_CODE (arg) == COMPONENT_REF)
  115.     {
  116.       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
  117.         fmt = "%s of member `%s' in read-only structure";
  118.       else
  119.         fmt = "%s of read-only member `%s'";
  120.       (*fn) (fmt, string, lang_printable_name (TREE_OPERAND (arg, 1)));
  121.     }
  122.   else if (TREE_CODE (arg) == VAR_DECL)
  123.     {
  124.       if (DECL_LANG_SPECIFIC (arg)
  125.       && DECL_IN_AGGR_P (arg)
  126.       && !TREE_STATIC (arg))
  127.     fmt = "%s of constant field `%s'";
  128.       else
  129.     fmt = "%s of read-only variable `%s'";
  130.       (*fn) (fmt, string, lang_printable_name (arg));
  131.     }
  132.   else if (TREE_CODE (arg) == PARM_DECL)
  133.     (*fn) ("%s of read-only parameter `%s'", string,
  134.        lang_printable_name (arg));
  135.   else if (TREE_CODE (arg) == INDIRECT_REF
  136.            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
  137.            && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
  138.                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
  139.     (*fn) ("%s of read-only reference `%s'",
  140.        string, lang_printable_name (TREE_OPERAND (arg, 0)));
  141.   else if (TREE_CODE (arg) == RESULT_DECL)
  142.     (*fn) ("%s of read-only named return value `%s'",
  143.        string, lang_printable_name (arg));
  144.   else           
  145.     (*fn) ("%s of read-only location", string);
  146. }
  147.  
  148. /* Print an error message for invalid use of a type which declares
  149.    virtual functions which are not inheritable.  */
  150. void
  151. abstract_virtuals_error (decl, type)
  152.      tree decl;
  153.      tree type;
  154. {
  155.   tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type);
  156.  
  157.   if (decl)
  158.     {
  159.       if (TREE_CODE (decl) == RESULT_DECL)
  160.     return;
  161.  
  162.       if (TREE_CODE (decl) == VAR_DECL)
  163.     cp_error ("cannot declare variable `%D' to be of type `%T'",
  164.             decl, type);
  165.       else if (TREE_CODE (decl) == PARM_DECL)
  166.     cp_error ("cannot declare parameter `%D' to be of type `%T'",
  167.             decl, type);
  168.       else if (TREE_CODE (decl) == FIELD_DECL)
  169.     cp_error ("cannot declare field `%D' to be of type `%T'",
  170.             decl, type);
  171.       else if (TREE_CODE (decl) == FUNCTION_DECL
  172.            && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
  173.     cp_error ("invalid return type for method `%#D'", decl);
  174.       else if (TREE_CODE (decl) == FUNCTION_DECL)
  175.     cp_error ("invalid return type for function `%#D'", decl);
  176.     }
  177.   else error ("cannot allocate an object of type `%T'", type);
  178.   /* Only go through this once.  */
  179.   if (TREE_PURPOSE (u) == NULL_TREE)
  180.     {
  181.       error ("  since the following virtual functions are abstract:");
  182.       TREE_PURPOSE (u) = error_mark_node;
  183.       while (u)
  184.     {
  185.       cp_error ("\t%#D", TREE_VALUE (u));
  186.       u = TREE_CHAIN (u);
  187.     }
  188.     }
  189.   else cp_error ("  since type `%T' has abstract virtual functions", type);
  190. }
  191.  
  192. /* Print an error message for invalid use of an incomplete type.
  193.    VALUE is the expression that was used (or 0 if that isn't known)
  194.    and TYPE is the type that was invalid.  */
  195.  
  196. void
  197. incomplete_type_error (value, type)
  198.      tree value;
  199.      tree type;
  200. {
  201.   char *errmsg;
  202.  
  203.   /* Avoid duplicate error message.  */
  204.   if (TREE_CODE (type) == ERROR_MARK)
  205.     return;
  206.  
  207.   if (value != 0 && (TREE_CODE (value) == VAR_DECL
  208.              || TREE_CODE (value) == PARM_DECL))
  209.     error ("`%s' has an incomplete type",
  210.        IDENTIFIER_POINTER (DECL_NAME (value)));
  211.   else
  212.     {
  213.     retry:
  214.       /* We must print an error message.  Be clever about what it says.  */
  215.  
  216.       switch (TREE_CODE (type))
  217.     {
  218.     case RECORD_TYPE:
  219.       errmsg = "invalid use of undefined type `struct %s'";
  220.       break;
  221.  
  222.     case UNION_TYPE:
  223.       errmsg = "invalid use of undefined type `union %s'";
  224.       break;
  225.  
  226.     case ENUMERAL_TYPE:
  227.       errmsg = "invalid use of undefined type `enum %s'";
  228.       break;
  229.  
  230.     case VOID_TYPE:
  231.       error ("invalid use of void expression");
  232.       return;
  233.  
  234.     case ARRAY_TYPE:
  235.       if (TYPE_DOMAIN (type))
  236.         {
  237.           type = TREE_TYPE (type);
  238.           goto retry;
  239.         }
  240.       error ("invalid use of array with unspecified bounds");
  241.       return;
  242.  
  243.     case OFFSET_TYPE:
  244.       error ("invalid use of member type (did you forget the `&' ?)");
  245.       return;
  246.  
  247.     default:
  248.       my_friendly_abort (108);
  249.     }
  250.  
  251.       error_with_aggr_type (type, errmsg);
  252.     }
  253. }
  254.  
  255. /* Like error(), but don't call report_error_function().  */
  256. static void
  257. ack (s, v, v2)
  258.      char *s;
  259.      HOST_WIDE_INT v;
  260.      HOST_WIDE_INT v2;
  261. {
  262.   extern char * progname;
  263.   
  264.   if (input_filename)
  265.     fprintf (stderr, "%s:%d: ", input_filename, lineno);
  266.   else
  267.     fprintf (stderr, "%s: ", progname);
  268.  
  269.   fprintf (stderr, s, v, v2);
  270.   fprintf (stderr, "\n");
  271. }
  272.   
  273. /* There are times when the compiler can get very confused, confused
  274.    to the point of giving up by aborting, simply because of previous
  275.    input errors.  It is much better to have the user go back and
  276.    correct those errors first, and see if it makes us happier, than it
  277.    is to abort on him.  This is because when one has a 10,000 line
  278.    program, and the compiler comes back with ``core dump'', the user
  279.    is left not knowing even where to begin to fix things and no place
  280.    to even try and work around things.
  281.  
  282.    The parameter is to uniquely identify the problem to the user, so
  283.    that they can say, I am having problem 59, and know that fix 7 will
  284.    probably solve their problem.  Or, we can document what problem
  285.    59 is, so they can understand how to work around it, should they
  286.    ever run into it.
  287.  
  288.    Note, there will be no more calls in the C++ front end to abort,
  289.    because the C++ front end is so unreliable still.  The C front end
  290.    can get away with calling abort, because for most of the calls to
  291.    abort on most machines, it, I suspect, can be proven that it is
  292.    impossible to ever call abort.  The same is not yet true for C++,
  293.    one day, maybe it will be.
  294.  
  295.    We used to tell people to "fix the above error[s] and try recompiling
  296.    the program" via a call to fatal, but that message tended to look
  297.    silly.  So instead, we just do the equivalent of a call to fatal in the
  298.    same situation (call exit).  */
  299.  
  300. /* First used: 0 (reserved), Last used: 355. Free: 145. */
  301.  
  302. static int abortcount = 0;
  303.  
  304. void
  305. my_friendly_abort (i)
  306.      int i;
  307. {
  308.   /* if the previous error came through here, i.e. report_error_function
  309.      ended up calling us again, don't just exit; we want a diagnostic of
  310.      some kind.  */
  311.   if (abortcount == 1)
  312.     current_function_decl = NULL_TREE;
  313.   else if (errorcount > 0 || sorrycount > 0)
  314.     {
  315.       if (abortcount > 1)
  316.     {
  317.       if (i == 0)
  318.         ack ("Internal compiler error.");
  319.       else
  320.         ack ("Internal compiler error %d.", i);
  321.       ack ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
  322.     }
  323.       else
  324.     error ("confused by earlier errors, bailing out");
  325.       
  326.       exit (34);
  327.     }
  328.   ++abortcount;
  329.  
  330.   if (i == 0)
  331.     error ("Internal compiler error.");
  332.   else
  333.     error ("Internal compiler error %d.", i);
  334.  
  335.   fatal ("Please submit a full bug report to `bug-g++@prep.ai.mit.edu'.");
  336. }
  337.  
  338. void
  339. my_friendly_assert (cond, where)
  340.      int cond, where;
  341. {
  342.   if (cond == 0)
  343.     my_friendly_abort (where);
  344. }
  345.  
  346. /* Return nonzero if VALUE is a valid constant-valued expression
  347.    for use in initializing a static variable; one that can be an
  348.    element of a "constant" initializer.
  349.  
  350.    Return 1 if the value is absolute; return 2 if it is relocatable.
  351.    We assume that VALUE has been folded as much as possible;
  352.    therefore, we do not need to check for such things as
  353.    arithmetic-combinations of integers.  */
  354.  
  355. static int
  356. initializer_constant_valid_p (value)
  357.      tree value;
  358. {
  359.   switch (TREE_CODE (value))
  360.     {
  361.     case CONSTRUCTOR:
  362.       return TREE_STATIC (value);
  363.  
  364.     case INTEGER_CST:
  365.     case REAL_CST:
  366.     case STRING_CST:
  367.       return 1;
  368.  
  369.     case ADDR_EXPR:
  370.       return 2;
  371.  
  372.     case CONVERT_EXPR:
  373.     case NOP_EXPR:
  374.       /* Allow conversions between types of the same kind.  */
  375.       if (TREE_CODE (TREE_TYPE (value))
  376.       == TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))))
  377.     return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  378.       /* Allow (int) &foo provided int is as wide as a pointer.  */
  379.       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
  380.       && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE
  381.       && ! tree_int_cst_lt (TYPE_SIZE (TREE_TYPE (value)),
  382.                 TYPE_SIZE (TREE_TYPE (TREE_OPERAND (value, 0)))))
  383.     return initializer_constant_valid_p (TREE_OPERAND (value, 0));
  384.       return 0;
  385.  
  386.     case PLUS_EXPR:
  387.       {
  388.     int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  389.     int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  390.     if (valid0 == 1 && valid1 == 2)
  391.       return 2;
  392.     if (valid0 == 2 && valid1 == 1)
  393.       return 2;
  394.     return 0;
  395.       }
  396.  
  397.     case MINUS_EXPR:
  398.       {
  399.     int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0));
  400.     int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1));
  401.     if (valid0 == 2 && valid1 == 1)
  402.       return 2;
  403.     return 0;
  404.       }
  405.     }
  406.  
  407.   return 0;
  408. }
  409.  
  410. /* Perform appropriate conversions on the initial value of a variable,
  411.    store it in the declaration DECL,
  412.    and print any error messages that are appropriate.
  413.    If the init is invalid, store an ERROR_MARK.
  414.  
  415.    C++: Note that INIT might be a TREE_LIST, which would mean that it is
  416.    a base class initializer for some aggregate type, hopefully compatible
  417.    with DECL.  If INIT is a single element, and DECL is an aggregate
  418.    type, we silently convert INIT into a TREE_LIST, allowing a constructor
  419.    to be called.
  420.  
  421.    If INIT is a TREE_LIST and there is no constructor, turn INIT
  422.    into a CONSTRUCTOR and use standard initialization techniques.
  423.    Perhaps a warning should be generated?
  424.  
  425.    Returns value of initializer if initialization could not be
  426.    performed for static variable.  In that case, caller must do
  427.    the storing.  */
  428.  
  429. tree
  430. store_init_value (decl, init)
  431.      tree decl, init;
  432. {
  433.   register tree value, type;
  434.  
  435.   /* If variable's type was invalidly declared, just ignore it.  */
  436.  
  437.   type = TREE_TYPE (decl);
  438.   if (TREE_CODE (type) == ERROR_MARK)
  439.     return NULL_TREE;
  440.  
  441.   /* Take care of C++ business up here.  */
  442.   type = TYPE_MAIN_VARIANT (type);
  443.  
  444.   /* implicitly tests if IS_AGGR_TYPE.  */
  445.   if (TYPE_NEEDS_CONSTRUCTING (type))
  446.     my_friendly_abort (109);
  447.   else if (IS_AGGR_TYPE (type))
  448.     {
  449.       /* @@ This may be wrong, but I do not know what is right.  */
  450.       if (TREE_CODE (init) == TREE_LIST)
  451.     {
  452.       cp_error ("constructor syntax used, but no constructor declared for type `%T'", type);
  453.       init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init));
  454.     }
  455. #if 0
  456.       if (TREE_CODE (init) == CONSTRUCTOR)
  457.     {
  458.       tree field;
  459.       tree funcs;
  460.       int func;
  461.  
  462.       /* Check that we're really an aggregate as ARM 8.4.1 defines it.  */
  463.       if (CLASSTYPE_N_BASECLASSES (type))
  464.         cp_error_at ("initializer list construction illegal for derived class object `%D'", decl);
  465.       if (CLASSTYPE_VTBL_PTR (type))
  466.         cp_error_at ("initializer list construction illegal for polymorphic class object `%D'", decl);
  467.       if (TYPE_HAS_CONSTRUCTOR (type))
  468.         {
  469.           cp_error_at ("initializer list construction illegal for `%D'", decl);
  470.           error ("due to the presence of a constructor");
  471.         }
  472.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  473.         if (TREE_PRIVATE (field) || TREE_PROTECTED (field))
  474.           {
  475.         cp_error_at ("initializer list construction illegal for `%D'", decl);
  476.         cp_error_at ("due to non-public visibility of member `%D'", field);
  477.           }
  478.       funcs = TYPE_METHODS (type);
  479.       if (funcs)
  480.         for (func = 0; func < TREE_VEC_LENGTH (funcs); func++)
  481.           {
  482.         field = TREE_VEC_ELT (funcs, func);
  483.         if (field && (TREE_PRIVATE (field) || TREE_PROTECTED (field)))
  484.           {
  485.             cp_error_at ("initializer list construction illegal for `%D'", decl);
  486.             cp_error_at ("due to non-public visibility of member `%D'", field);
  487.           }
  488.           }
  489.     }
  490. #endif
  491.     }
  492.   else if (TREE_CODE (init) == TREE_LIST
  493.        && TREE_TYPE (init) != unknown_type_node)
  494.     {
  495.       if (TREE_CODE (decl) == RESULT_DECL)
  496.     {
  497.       if (TREE_CHAIN (init))
  498.         {
  499.           warning ("comma expression used to initialize return value");
  500.           init = build_compound_expr (init);
  501.         }
  502.       else
  503.         init = TREE_VALUE (init);
  504.     }
  505.       else if (TREE_TYPE (init) != 0
  506.            && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE)
  507.     {
  508.       /* Use the type of our variable to instantiate
  509.          the type of our initializer.  */
  510.       init = instantiate_type (type, init, 1);
  511.     }
  512.       else if (TREE_CODE (init) == TREE_LIST
  513.            && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
  514.     {
  515.       error ("cannot initialize arrays using this syntax");
  516.       return NULL_TREE;
  517.     }
  518.       else
  519.     {
  520.       error ("bad syntax in initialization");
  521.       return NULL_TREE;
  522.     }
  523.     }
  524.  
  525.   /* End of special C++ code.  */
  526.  
  527.   /* Digest the specified initializer into an expression.  */
  528.  
  529.   value = digest_init (type, init, (tree *) 0);
  530.  
  531.   /* Store the expression if valid; else report error.  */
  532.  
  533.   if (TREE_CODE (value) == ERROR_MARK)
  534.     ;
  535.   else if (TREE_STATIC (decl)
  536.        && (! TREE_CONSTANT (value)
  537.            || ! initializer_constant_valid_p (value)
  538.            /* Since ctors and dtors are the only things that can
  539.           reference vtables, and they are always written down
  540.           the the vtable definition, we can leave the
  541.           vtables in initialized data space.
  542.           However, other initialized data cannot be initialized
  543.           this way.  Instead a global file-level initializer
  544.           must do the job.  */
  545.            || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl))))
  546.     return value;
  547.   else
  548.     {
  549.       if (pedantic && TREE_CODE (value) == CONSTRUCTOR)
  550.     {
  551.       if (! TREE_CONSTANT (value) || ! TREE_STATIC (value))
  552.         pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions");
  553.     }
  554.     }
  555.   DECL_INITIAL (decl) = value;
  556.   return NULL_TREE;
  557. }
  558.  
  559. /* Digest the parser output INIT as an initializer for type TYPE.
  560.    Return a C expression of type TYPE to represent the initial value.
  561.  
  562.    If TAIL is nonzero, it points to a variable holding a list of elements
  563.    of which INIT is the first.  We update the list stored there by
  564.    removing from the head all the elements that we use.
  565.    Normally this is only one; we use more than one element only if
  566.    TYPE is an aggregate and INIT is not a constructor.  */
  567.  
  568. tree
  569. digest_init (type, init, tail)
  570.      tree type, init, *tail;
  571. {
  572.   enum tree_code code = TREE_CODE (type);
  573.   tree element = 0;
  574.   tree old_tail_contents;
  575.   /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR
  576.      tree node which has no TREE_TYPE.  */
  577.   int raw_constructor;
  578.  
  579.   /* By default, assume we use one element from a list.
  580.      We correct this later in the sole case where it is not true.  */
  581.  
  582.   if (tail)
  583.     {
  584.       old_tail_contents = *tail;
  585.       *tail = TREE_CHAIN (*tail);
  586.     }
  587.  
  588.   if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
  589.                   && TREE_VALUE (init) == error_mark_node))
  590.     return error_mark_node;
  591.  
  592.   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
  593.   if (TREE_CODE (init) == NON_LVALUE_EXPR)
  594.     init = TREE_OPERAND (init, 0);
  595.  
  596.   if (init && TYPE_PTRMEMFUNC_P (type)
  597.       && ((TREE_CODE (init) == ADDR_EXPR
  598.        && TREE_CODE (TREE_TYPE (init)) == POINTER_TYPE
  599.        && TREE_CODE (TREE_TYPE (TREE_TYPE (init))) == METHOD_TYPE)
  600.       || integer_zerop (init)))
  601.     {
  602.       init = build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), init);
  603.     }
  604.  
  605.   raw_constructor = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0;
  606.  
  607.   if (init && raw_constructor
  608.       && CONSTRUCTOR_ELTS (init) != 0
  609.       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
  610.     {
  611.       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
  612.       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
  613.       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
  614.     element = TREE_OPERAND (element, 0);
  615.       if (element == error_mark_node)
  616.     return element;
  617.     }
  618.  
  619.   /* Any type can be initialized from an expression of the same type,
  620.      optionally with braces.  */
  621.  
  622.   if (init && TREE_TYPE (init)
  623.       && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type
  624.       || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1))))
  625.     {
  626.       if (pedantic && code == ARRAY_TYPE
  627.       && TREE_CODE (init) != STRING_CST)
  628.     pedwarn ("ANSI C++ forbids initializing array from array expression");
  629.       if (TREE_CODE (init) == CONST_DECL)
  630.     init = DECL_INITIAL (init);
  631.       else if (TREE_READONLY_DECL_P (init))
  632.     init = decl_constant_value (init);
  633.       return init;
  634.     }
  635.  
  636.   if (element && (TREE_TYPE (element) == type
  637.           || (code == ARRAY_TYPE && TREE_TYPE (element)
  638.               && comptypes (TREE_TYPE (element), type, 1))))
  639.     {
  640.       if (pedantic && code == ARRAY_TYPE)
  641.     pedwarn ("ANSI C++ forbids initializing array from array expression");
  642.       if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE))
  643.     pedwarn ("ANSI C++ forbids single nonscalar initializer with braces");
  644.       if (TREE_CODE (element) == CONST_DECL)
  645.     element = DECL_INITIAL (element);
  646.       else if (TREE_READONLY_DECL_P (element))
  647.     element = decl_constant_value (element);
  648.       return element;
  649.     }
  650.  
  651.   /* Check for initializing a union by its first field.
  652.      Such an initializer must use braces.  */
  653.  
  654.   if (code == UNION_TYPE)
  655.     {
  656.       tree result, field = TYPE_FIELDS (type);
  657.  
  658.       /* Find the first named field.  ANSI decided in September 1990
  659.      that only named fields count here.  */
  660.       while (field && DECL_NAME (field) == 0)
  661.     field = TREE_CHAIN (field);
  662.  
  663.       if (field == 0)
  664.     {
  665.       error ("union with no named members cannot be initialized");
  666.       return error_mark_node;
  667.     }
  668.  
  669.       if (raw_constructor && !TYPE_NEEDS_CONSTRUCTING (type))
  670.     {
  671.       result = process_init_constructor (type, init, NULL_PTR);
  672.       return result;
  673.     }
  674.  
  675.       if (! raw_constructor)
  676.     {
  677.       error ("type mismatch in initialization");
  678.       return error_mark_node;
  679.     }
  680.       if (element == 0)
  681.     {
  682.       if (!TYPE_NEEDS_CONSTRUCTING (type))
  683.         {
  684.           error ("union initializer requires one element");
  685.           return error_mark_node;
  686.         }
  687.     }
  688.       else
  689.     {
  690.       /* Take just the first element from within the constructor
  691.          and it should match the type of the first element.  */
  692.       element = digest_init (TREE_TYPE (field), element, (tree *) 0);
  693.       result = build (CONSTRUCTOR, type, 0, build_tree_list (field, element));
  694.       TREE_CONSTANT (result) = TREE_CONSTANT (element);
  695.       TREE_STATIC (result) = (initializer_constant_valid_p (element)
  696.                   && TREE_CONSTANT (element));
  697.       return result;
  698.     }
  699.     }
  700.  
  701.   /* Initialization of an array of chars from a string constant
  702.      optionally enclosed in braces.  */
  703.  
  704.   if (code == ARRAY_TYPE)
  705.     {
  706.       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  707.       if ((typ1 == char_type_node
  708.        || typ1 == signed_char_type_node
  709.        || typ1 == unsigned_char_type_node
  710.        || typ1 == unsigned_wchar_type_node
  711.        || typ1 == signed_wchar_type_node)
  712.       && ((init && TREE_CODE (init) == STRING_CST)
  713.           || (element && TREE_CODE (element) == STRING_CST)))
  714.     {
  715.       tree string = element ? element : init;
  716.  
  717.       if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
  718.            != char_type_node)
  719.           && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
  720.         {
  721.           error ("char-array initialized from wide string");
  722.           return error_mark_node;
  723.         }
  724.       if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
  725.            == char_type_node)
  726.           && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
  727.         {
  728.           error ("int-array initialized from non-wide string");
  729.           return error_mark_node;
  730.         }
  731.  
  732.       if (pedantic && typ1 != char_type_node)
  733.         pedwarn ("ANSI C++ forbids string initializer except for `char' elements");
  734.       TREE_TYPE (string) = type;
  735.       if (TYPE_DOMAIN (type) != 0
  736.           && TREE_CONSTANT (TYPE_SIZE (type)))
  737.         {
  738.           register int size
  739.         = TREE_INT_CST_LOW (TYPE_SIZE (type));
  740.           size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  741.           /* In C it is ok to subtract 1 from the length of the string
  742.          because it's ok to ignore the terminating null char that is
  743.          counted in the length of the constant, but in C++ this would
  744.          be invalid.  */
  745.           if (size < TREE_STRING_LENGTH (string))
  746.         warning ("initializer-string for array of chars is too long");
  747.         }
  748.       return string;
  749.     }
  750.     }
  751.  
  752.   /* Handle scalar types, including conversions.  */
  753.  
  754.   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
  755.       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE)
  756.     {
  757.       if (raw_constructor)
  758.     {
  759.       if (element == 0)
  760.         {
  761.           error ("initializer for scalar variable requires one element");
  762.           return error_mark_node;
  763.         }
  764.       init = element;
  765.     }
  766.  
  767.       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
  768.                      "initialization", NULL_TREE, 0);
  769.     }
  770.  
  771.   /* Come here only for records and arrays (and unions with constructors).  */
  772.  
  773.   if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
  774.     {
  775.       error ("variable-sized object may not be initialized");
  776.       return error_mark_node;
  777.     }
  778.  
  779.   if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE)
  780.     {
  781.       if (raw_constructor)
  782.     return process_init_constructor (type, init, (tree *)0);
  783.       else if (TYPE_NEEDS_CONSTRUCTING (type))
  784.     {
  785.       /* This can only be reached when caller is initializing
  786.          ARRAY_TYPE.  In that case, we don't want to convert
  787.          INIT to TYPE.  We will let `expand_vec_init' do it.  */
  788.       return init;
  789.     }
  790.       else if (tail != 0)
  791.     {
  792.       *tail = old_tail_contents;
  793.       return process_init_constructor (type, 0, tail);
  794.     }
  795.       else if (flag_traditional)
  796.     /* Traditionally one can say `char x[100] = 0;'.  */
  797.     return process_init_constructor (type,
  798.                      build_nt (CONSTRUCTOR, 0,
  799.                            tree_cons (0, init, 0)),
  800.                      0);
  801.       if (code != ARRAY_TYPE)
  802.     return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
  803.                        "initialization", NULL_TREE, 0);
  804.     }
  805.  
  806.   error ("invalid initializer");
  807.   return error_mark_node;
  808. }
  809.  
  810. /* Process a constructor for a variable of type TYPE.
  811.    The constructor elements may be specified either with INIT or with ELTS,
  812.    only one of which should be non-null.
  813.  
  814.    If INIT is specified, it is a CONSTRUCTOR node which is specifically
  815.    and solely for initializing this datum.
  816.  
  817.    If ELTS is specified, it is the address of a variable containing
  818.    a list of expressions.  We take as many elements as we need
  819.    from the head of the list and update the list.
  820.  
  821.    In the resulting constructor, TREE_CONSTANT is set if all elts are
  822.    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
  823.    constants that the assembler and linker can compute them.  */
  824.  
  825. static tree
  826. process_init_constructor (type, init, elts)
  827.      tree type, init, *elts;
  828. {
  829.   extern tree empty_init_node;
  830.   register tree tail;
  831.   /* List of the elements of the result constructor,
  832.      in reverse order.  */
  833.   register tree members = NULL;
  834.   tree result;
  835.   int allconstant = 1;
  836.   int allsimple = 1;
  837.   int erroneous = 0;
  838.  
  839.   /* Make TAIL be the list of elements to use for the initialization,
  840.      no matter how the data was given to us.  */
  841.  
  842.   if (elts)
  843.     {
  844.       if (warn_missing_braces)
  845.     warning ("aggregate has a partly bracketed initializer");
  846.       tail = *elts;
  847.     }
  848.   else
  849.     tail = CONSTRUCTOR_ELTS (init);
  850.  
  851.   /* Gobble as many elements as needed, and make a constructor or initial value
  852.      for each element of this aggregate.  Chain them together in result.
  853.      If there are too few, use 0 for each scalar ultimate component.  */
  854.  
  855.   if (TREE_CODE (type) == ARRAY_TYPE)
  856.     {
  857.       tree domain = TYPE_DOMAIN (type);
  858.       register long len;
  859.       register int i;
  860.  
  861.       if (domain)
  862.     len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
  863.            - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
  864.            + 1);
  865.       else
  866.     len = -1;  /* Take as many as there are */
  867.  
  868.       for (i = 0; (len < 0 || i < len) && tail != 0; i++)
  869.     {
  870.       register tree next1;
  871.  
  872.       if (TREE_VALUE (tail) != 0)
  873.         {
  874.           tree tail1 = tail;
  875.           next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
  876.                    TREE_VALUE (tail), &tail1);
  877.           my_friendly_assert (tail1 == 0
  878.                   || TREE_CODE (tail1) == TREE_LIST, 319);
  879.           if (tail == tail1 && len < 0)
  880.         {
  881.           error ("non-empty initializer for array of empty elements");
  882.           /* Just ignore what we were supposed to use.  */
  883.           tail1 = 0;
  884.         }
  885.           tail = tail1;
  886.         }
  887.       else
  888.         {
  889.           next1 = error_mark_node;
  890.           tail = TREE_CHAIN (tail);
  891.         }
  892.  
  893.       if (next1 == error_mark_node)
  894.         erroneous = 1;
  895.       else if (!TREE_CONSTANT (next1))
  896.         allconstant = 0;
  897.       else if (! initializer_constant_valid_p (next1))
  898.         allsimple = 0;
  899.       members = tree_cons (NULL_TREE, next1, members);
  900.     }
  901.     }
  902.   if (TREE_CODE (type) == RECORD_TYPE && init != empty_init_node)
  903.     {
  904.       register tree field;
  905.  
  906.       if (tail)
  907.     {
  908.       if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  909.         {
  910.           sorry ("initializer list for object of class with virtual baseclasses");
  911.           return error_mark_node;
  912.         }
  913.  
  914.       if (TYPE_BINFO_BASETYPES (type))
  915.         {
  916.           sorry ("initializer list for object of class with baseclasses");
  917.           return error_mark_node;
  918.         }
  919.  
  920.       if (TYPE_VIRTUAL_P (type))
  921.         {
  922.           sorry ("initializer list for object using virtual functions");
  923.           return error_mark_node;
  924.         }
  925.     }
  926.  
  927.       for (field = TYPE_FIELDS (type); field && tail;
  928.        field = TREE_CHAIN (field))
  929.     {
  930.       register tree next1;
  931.  
  932.       if (! DECL_NAME (field))
  933.         {
  934.           members = tree_cons (field, integer_zero_node, members);
  935.           continue;
  936.         }
  937.  
  938.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  939.         continue;
  940.  
  941.       /* A static member isn't considered "part of the object", so
  942.          it has no business even thinking about involving itself in
  943.          what an initializer-list is trying to do.  */
  944.       if (TREE_CODE (field) == VAR_DECL && TREE_STATIC (field))
  945.         continue;
  946.  
  947.       if (TREE_VALUE (tail) != 0)
  948.         {
  949.           tree tail1 = tail;
  950.  
  951.           if (TYPE_PTRMEMFUNC_P (TREE_TYPE (field)))
  952.         {
  953.           tree t
  954.             = build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (field)),
  955.                     default_conversion (TREE_VALUE (tail)));
  956.           if (t == NULL_TREE)
  957.             return error_mark_node;
  958.           next1 = digest_init (TREE_TYPE (field), t, &tail1);
  959.         }
  960.           else
  961.         next1 = digest_init (TREE_TYPE (field),
  962.                      TREE_VALUE (tail), &tail1);
  963.           my_friendly_assert (tail1 == 0
  964.                   || TREE_CODE (tail1) == TREE_LIST, 320);
  965.           tail = tail1;
  966.         }
  967.       else
  968.         {
  969.           next1 = error_mark_node;
  970.           tail = TREE_CHAIN (tail);
  971.         }
  972.  
  973.       if (next1 == error_mark_node)
  974.         erroneous = 1;
  975.       else if (!TREE_CONSTANT (next1))
  976.         allconstant = 0;
  977.       else if (! initializer_constant_valid_p (next1))
  978.         allsimple = 0;
  979.       members = tree_cons (field, next1, members);
  980.     }
  981.       for (; field; field = TREE_CHAIN (field))
  982.     {
  983.       if (TREE_CODE (field) != FIELD_DECL)
  984.         continue;
  985.  
  986.       /* Does this field have a default initialization?  */
  987.       if (DECL_INITIAL (field))
  988.         {
  989.           register tree next1 = DECL_INITIAL (field);
  990.           if (TREE_CODE (next1) == ERROR_MARK)
  991.         erroneous = 1;
  992.           else if (!TREE_CONSTANT (next1))
  993.         allconstant = 0;
  994.           else if (! initializer_constant_valid_p (next1))
  995.         allsimple = 0;
  996.           members = tree_cons (field, next1, members);
  997.         }
  998.       else if (TREE_READONLY (field))
  999.         error ("uninitialized const member `%s'",
  1000.            IDENTIFIER_POINTER (DECL_NAME (field)));
  1001.       else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field))
  1002.            && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
  1003.         error ("member `%s' with uninitialized const fields",
  1004.            IDENTIFIER_POINTER (DECL_NAME (field)));
  1005.       else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
  1006.         error ("member `%s' is uninitialized reference",
  1007.            IDENTIFIER_POINTER (DECL_NAME (field)));
  1008.     }
  1009.     }
  1010.  
  1011.   if (TREE_CODE (type) == UNION_TYPE)
  1012.     {
  1013.       register tree field = TYPE_FIELDS (type);
  1014.       register tree next1;
  1015.  
  1016.       /* Find the first named field.  ANSI decided in September 1990
  1017.      that only named fields count here.  */
  1018.       while (field && DECL_NAME (field) == 0)
  1019.     field = TREE_CHAIN (field);
  1020.  
  1021.       /* For a union, get the initializer for 1 fld.  */
  1022.  
  1023.       if (tail == NULL_TREE)
  1024.     {
  1025.       error ("empty initializer for union");
  1026.       tail = build_tree_list (NULL_TREE, NULL_TREE);
  1027.     }
  1028.  
  1029.       /* If this element specifies a field, initialize via that field.  */
  1030.       if (TREE_PURPOSE (tail) != NULL_TREE)
  1031.     {
  1032.       int win = 0;
  1033.  
  1034.       if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
  1035.         /* Handle the case of a call by build_c_cast.  */
  1036.         field = TREE_PURPOSE (tail), win = 1;
  1037.       else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
  1038.         error ("index value instead of field name in union initializer");
  1039.       else
  1040.         {
  1041.           tree temp;
  1042.           for (temp = TYPE_FIELDS (type);
  1043.            temp;
  1044.            temp = TREE_CHAIN (temp))
  1045.         if (DECL_NAME (temp) == TREE_PURPOSE (tail))
  1046.           break;
  1047.           if (temp)
  1048.         field = temp, win = 1;
  1049.           else
  1050.         error ("no field `%s' in union being initialized",
  1051.                IDENTIFIER_POINTER (TREE_PURPOSE (tail)));
  1052.         }
  1053.       if (!win)
  1054.         TREE_VALUE (tail) = error_mark_node;
  1055.     }
  1056.  
  1057.       if (TREE_VALUE (tail) != 0)
  1058.     {
  1059.       tree tail1 = tail;
  1060.  
  1061.       next1 = digest_init (TREE_TYPE (field),
  1062.                    TREE_VALUE (tail), &tail1);
  1063.       if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST)
  1064.         abort ();
  1065.       tail = tail1;
  1066.     }
  1067.       else
  1068.     {
  1069.       next1 = error_mark_node;
  1070.       tail = TREE_CHAIN (tail);
  1071.     }
  1072.  
  1073.       if (next1 == error_mark_node)
  1074.     erroneous = 1;
  1075.       else if (!TREE_CONSTANT (next1))
  1076.     allconstant = 0;
  1077.       else if (initializer_constant_valid_p (next1) == 0)
  1078.     allsimple = 0;
  1079.       members = tree_cons (field, next1, members);
  1080.     }
  1081.  
  1082.   /* If arguments were specified as a list, just remove the ones we used.  */
  1083.   if (elts)
  1084.     *elts = tail;
  1085.   /* If arguments were specified as a constructor,
  1086.      complain unless we used all the elements of the constructor.  */
  1087.   else if (tail)
  1088.     warning ("excess elements in aggregate initializer");
  1089.  
  1090.   if (erroneous)
  1091.     return error_mark_node;
  1092.  
  1093.   result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members));
  1094.   if (init)
  1095.     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
  1096.   if (allconstant) TREE_CONSTANT (result) = 1;
  1097.   if (allconstant && allsimple) TREE_STATIC (result) = 1;
  1098.   return result;
  1099. }
  1100.  
  1101. /* Given a structure or union value DATUM, construct and return
  1102.    the structure or union component which results from narrowing
  1103.    that value by the types specified in TYPES.  For example, given the
  1104.    hierarchy
  1105.  
  1106.    class L { int ii; };
  1107.    class A : L { ... };
  1108.    class B : L { ... };
  1109.    class C : A, B { ... };
  1110.  
  1111.    and the declaration
  1112.  
  1113.    C x;
  1114.  
  1115.    then the expression
  1116.  
  1117.    x::C::A::L::ii refers to the ii member of the L part of
  1118.    of A part of the C object named by X.  In this case,
  1119.    DATUM would be x, and TYPES would be a SCOPE_REF consisting of
  1120.  
  1121.     SCOPE_REF
  1122.         SCOPE_REF
  1123.             C    A
  1124.         L
  1125.  
  1126.    The last entry in the SCOPE_REF is always an IDENTIFIER_NODE.
  1127.  
  1128. */
  1129.  
  1130. tree
  1131. build_scoped_ref (datum, types)
  1132.      tree datum;
  1133.      tree types;
  1134. {
  1135.   tree ref;
  1136.   tree type = TREE_TYPE (datum);
  1137.  
  1138.   if (datum == error_mark_node)
  1139.     return error_mark_node;
  1140.  
  1141.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1142.     type = TREE_TYPE (type);
  1143.  
  1144.   type = TYPE_MAIN_VARIANT (type);
  1145.  
  1146.   if (TREE_CODE (types) == SCOPE_REF)
  1147.     {
  1148.       /* We have some work to do.  */
  1149.       struct type_chain { tree type; struct type_chain *next; } *chain = 0, *head = 0, scratch;
  1150.       ref = build_unary_op (ADDR_EXPR, datum, 0);
  1151.       while (TREE_CODE (types) == SCOPE_REF)
  1152.     {
  1153.       tree t = TREE_OPERAND (types, 1);
  1154.       if (is_aggr_typedef (t, 1))
  1155.         {
  1156.           head = (struct type_chain *)alloca (sizeof (struct type_chain));
  1157.           head->type = IDENTIFIER_TYPE_VALUE (t);
  1158.           head->next = chain;
  1159.           chain = head;
  1160.           types = TREE_OPERAND (types, 0);
  1161.         }
  1162.       else return error_mark_node;
  1163.     }
  1164.       if (! is_aggr_typedef (types, 1))
  1165.     return error_mark_node;
  1166.  
  1167.       head = &scratch;
  1168.       head->type = IDENTIFIER_TYPE_VALUE (types);
  1169.       head->next = chain;
  1170.       chain = head;
  1171.       while (chain)
  1172.     {
  1173.       tree binfo = chain->type;
  1174.       type = TREE_TYPE (TREE_TYPE (ref));
  1175.       if (binfo != TYPE_BINFO (type))
  1176.         {
  1177.           binfo = get_binfo (binfo, type, 1);
  1178.           if (binfo == error_mark_node)
  1179.         return error_mark_node;
  1180.           if (binfo == 0)
  1181.         return error_not_base_type (chain->type, type);
  1182.           ref = convert_pointer_to (binfo, ref);
  1183.         }
  1184.       chain = chain->next;
  1185.     }
  1186.       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
  1187.     }
  1188.  
  1189.   /* This is an easy conversion.  */
  1190.   if (is_aggr_typedef (types, 1))
  1191.     {
  1192.       tree binfo = TYPE_BINFO (IDENTIFIER_TYPE_VALUE (types));
  1193.       if (binfo != TYPE_BINFO (type))
  1194.     {
  1195.       binfo = get_binfo (binfo, type, 1);
  1196.       if (binfo == error_mark_node)
  1197.         return error_mark_node;
  1198.       if (binfo == 0)
  1199.         return error_not_base_type (IDENTIFIER_TYPE_VALUE (types), type);
  1200.     }
  1201.  
  1202.       switch (TREE_CODE (datum))
  1203.     {
  1204.     case NOP_EXPR:
  1205.     case CONVERT_EXPR:
  1206.     case FLOAT_EXPR:
  1207.     case FIX_TRUNC_EXPR:
  1208.     case FIX_FLOOR_EXPR:
  1209.     case FIX_ROUND_EXPR:
  1210.     case FIX_CEIL_EXPR:
  1211.       ref = convert_pointer_to (binfo,
  1212.                     build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0));
  1213.       break;
  1214.     default:
  1215.       ref = convert_pointer_to (binfo,
  1216.                     build_unary_op (ADDR_EXPR, datum, 0));
  1217.     }
  1218.       return build_indirect_ref (ref, "(compiler error in build_scoped_ref)");
  1219.     }
  1220.   return error_mark_node;
  1221. }
  1222.  
  1223. /* Build a reference to an object specified by the C++ `->' operator.
  1224.    Usually this just involves dereferencing the object, but if the
  1225.    `->' operator is overloaded, then such overloads must be
  1226.    performed until an object which does not have the `->' operator
  1227.    overloaded is found.  An error is reported when circular pointer
  1228.    delegation is detected.  */
  1229. tree
  1230. build_x_arrow (datum)
  1231.      tree datum;
  1232. {
  1233.   tree types_memoized = NULL_TREE;
  1234.   register tree rval = datum;
  1235.   tree type = TREE_TYPE (rval);
  1236.   tree last_rval;
  1237.  
  1238.   if (type == error_mark_node)
  1239.     return error_mark_node;
  1240.  
  1241.   if (TREE_CODE (type) == REFERENCE_TYPE)
  1242.     {
  1243.       rval = convert_from_reference (rval);
  1244.       type = TREE_TYPE (rval);
  1245.     }
  1246.  
  1247.   if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (type))
  1248.     {
  1249.       while (rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval, NULL_TREE, NULL_TREE))
  1250.     {
  1251.       if (rval == error_mark_node)
  1252.         return error_mark_node;
  1253.  
  1254.       if (value_member (TREE_TYPE (rval), types_memoized))
  1255.         {
  1256.           error ("circular pointer delegation detected");
  1257.           return error_mark_node;
  1258.         }
  1259.       else
  1260.         {
  1261.           types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval),
  1262.                       types_memoized);
  1263.         }
  1264.       last_rval = rval;
  1265.     }     
  1266.       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
  1267.     last_rval = convert_from_reference (last_rval);
  1268.     }
  1269.   else
  1270.     last_rval = default_conversion (rval);
  1271.  
  1272.  more:
  1273.   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
  1274.     return build_indirect_ref (last_rval, NULL_PTR);
  1275.  
  1276.   if (TREE_CODE (TREE_TYPE (last_rval)) == OFFSET_TYPE)
  1277.     {
  1278.       if (TREE_CODE (last_rval) == OFFSET_REF
  1279.       && TREE_STATIC (TREE_OPERAND (last_rval, 1)))
  1280.     {
  1281.       last_rval = TREE_OPERAND (last_rval, 1);
  1282.       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
  1283.         last_rval = convert_from_reference (last_rval);
  1284.       goto more;
  1285.     }
  1286.       compiler_error ("invalid member type in build_x_arrow");
  1287.       return error_mark_node;
  1288.     }
  1289.  
  1290.   if (types_memoized)
  1291.     error ("result of `operator->()' yields non-pointer result");
  1292.   else
  1293.     error ("base operand of `->' is not a pointer");
  1294.   return error_mark_node;
  1295. }
  1296.  
  1297. /* Make an expression to refer to the COMPONENT field of
  1298.    structure or union value DATUM.  COMPONENT is an arbitrary
  1299.    expression.  DATUM has already been checked out to be of
  1300.    aggregate type.
  1301.  
  1302.    For C++, COMPONENT may be a TREE_LIST.  This happens when we must
  1303.    return an object of member type to a method of the current class,
  1304.    but there is not yet enough typing information to know which one.
  1305.    As a special case, if there is only one method by that name,
  1306.    it is returned.  Otherwise we return an expression which other
  1307.    routines will have to know how to deal with later.  */
  1308. tree
  1309. build_m_component_ref (datum, component)
  1310.      tree datum, component;
  1311. {
  1312.   tree type;
  1313.   tree objtype = TREE_TYPE (datum);
  1314.   tree rettype;
  1315.  
  1316.   if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component)))
  1317.     {
  1318.       type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component)));
  1319.       rettype = type;
  1320.     }
  1321.   else
  1322.     {
  1323.       component = build_indirect_ref (component, NULL_PTR);
  1324.       type = TREE_TYPE (component);
  1325.       rettype = TREE_TYPE (TREE_TYPE (component));
  1326.     }
  1327.  
  1328.   if (datum == error_mark_node || component == error_mark_node)
  1329.     return error_mark_node;
  1330.  
  1331.   if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE)
  1332.     {
  1333.       error ("non-member type composed with object");
  1334.       return error_mark_node;
  1335.     }
  1336.  
  1337.   if (TREE_CODE (objtype) == REFERENCE_TYPE)
  1338.     objtype = TREE_TYPE (objtype);
  1339.  
  1340.   if (! comptypes (TYPE_METHOD_BASETYPE (type), objtype, 0))
  1341.     {
  1342.       error ("member type `%s::' incompatible with object type `%s'",
  1343.          TYPE_NAME_STRING (TYPE_METHOD_BASETYPE (type)),
  1344.          TYPE_NAME_STRING (objtype));
  1345.       return error_mark_node;
  1346.     }
  1347.  
  1348.   return build (OFFSET_REF, rettype, datum, component);
  1349. }
  1350.  
  1351. /* Return a tree node for the expression TYPENAME '(' PARMS ')'.
  1352.  
  1353.    Because we cannot tell whether this construct is really
  1354.    a function call or a call to a constructor or a request for
  1355.    a type conversion, we try all three, and report any ambiguities
  1356.    we find.  */
  1357. tree
  1358. build_functional_cast (exp, parms)
  1359.      tree exp;
  1360.      tree parms;
  1361. {
  1362.   /* This is either a call to a constructor,
  1363.      or a C cast in C++'s `functional' notation.  */
  1364.   tree type, name = NULL_TREE;
  1365.   tree expr_as_ctor = NULL_TREE;
  1366.   tree expr_as_method = NULL_TREE;
  1367.   tree expr_as_fncall = NULL_TREE;
  1368.   tree expr_as_conversion = NULL_TREE;
  1369.  
  1370.   if (exp == error_mark_node || parms == error_mark_node)
  1371.     return error_mark_node;
  1372.  
  1373.   if (TREE_CODE (exp) == IDENTIFIER_NODE)
  1374.     {
  1375.       name = exp;
  1376.  
  1377.       if (IDENTIFIER_HAS_TYPE_VALUE (exp))
  1378.     /* Either an enum or an aggregate type.  */
  1379.     type = IDENTIFIER_TYPE_VALUE (exp);
  1380.       else
  1381.     {
  1382.       type = lookup_name (exp, 1);
  1383.       if (!type || TREE_CODE (type) != TYPE_DECL)
  1384.         {
  1385.           error ("`%s' fails to be a typedef or built-in type",
  1386.              IDENTIFIER_POINTER (name));
  1387.           return error_mark_node;
  1388.         }
  1389.       type = TREE_TYPE (type);
  1390.     }
  1391.     }
  1392.   else type = exp;
  1393.  
  1394.   /* Prepare to evaluate as a call to a constructor.  If this expression
  1395.      is actually used, for example,
  1396.      
  1397.      return X (arg1, arg2, ...);
  1398.      
  1399.      then the slot being initialized will be filled in.  */
  1400.  
  1401.   if (name == NULL_TREE)
  1402.     {
  1403.       name = TYPE_NAME (type);
  1404.       if (TREE_CODE (name) == TYPE_DECL)
  1405.     name = DECL_NAME (name);
  1406.     }
  1407.  
  1408.   /* Try evaluating as a call to a function.  */
  1409.   if (IDENTIFIER_CLASS_VALUE (name))
  1410.     {
  1411.       expr_as_method = build_method_call (current_class_decl, name, parms,
  1412.                       NULL_TREE, LOOKUP_SPECULATIVELY);
  1413.       if (expr_as_method == error_mark_node)
  1414.     expr_as_method = NULL_TREE;
  1415.     }
  1416.  
  1417.   if (IDENTIFIER_GLOBAL_VALUE (name)
  1418.       && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST
  1419.       || TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL))
  1420.     {
  1421.       expr_as_fncall = build_overload_call (name, parms, 0,
  1422.                         (struct candidate *)0);
  1423.       if (expr_as_fncall == NULL_TREE)
  1424.     expr_as_fncall = error_mark_node;
  1425.     }
  1426.  
  1427.   if (! IS_AGGR_TYPE (type))
  1428.     {
  1429.       /* this must build a C cast */
  1430.       if (parms == NULL_TREE)
  1431.     {
  1432.       if (expr_as_method || expr_as_fncall)
  1433.         goto return_function;
  1434.  
  1435.       cp_error ("cannot cast null list to type `%#T'", type);
  1436.       return error_mark_node;
  1437.     }
  1438.       if (expr_as_method
  1439.       || (expr_as_fncall && expr_as_fncall != error_mark_node))
  1440.     {
  1441.       cp_error ("ambiguity between cast to `%#T' and function call", type);
  1442.       return error_mark_node;
  1443.     }
  1444.       return build_c_cast (type, build_compound_expr (parms));
  1445.     }
  1446.  
  1447.   if (TYPE_SIZE (type) == NULL_TREE)
  1448.     {
  1449.       if (expr_as_method || expr_as_fncall)
  1450.     goto return_function;
  1451.       error ("type `%s' is not yet defined", IDENTIFIER_POINTER (name));
  1452.       return error_mark_node;
  1453.     }
  1454.  
  1455.   if (parms && TREE_CHAIN (parms) == NULL_TREE)
  1456.     expr_as_conversion
  1457.       = build_type_conversion (CONVERT_EXPR, type, TREE_VALUE (parms), 0);
  1458.     
  1459.   if (! TYPE_NEEDS_CONSTRUCTOR (type) && parms != NULL_TREE)
  1460.     {
  1461.       char *msg = 0;
  1462.  
  1463.       if (parms == NULL_TREE)
  1464.     msg = "argument missing in cast to `%s' type";
  1465.       else if (TREE_CHAIN (parms) == NULL_TREE)
  1466.     {
  1467.       if (expr_as_conversion == NULL_TREE)
  1468.         msg = "conversion to type `%s' failed";
  1469.     }
  1470.       else msg = "type `%s' does not have a constructor";
  1471.  
  1472.       if ((expr_as_method || expr_as_fncall) && expr_as_conversion)
  1473.     msg = "ambiguity between conversion to `%s' and function call";
  1474.       else if (expr_as_method || expr_as_fncall)
  1475.     goto return_function;
  1476.       else if (expr_as_conversion)
  1477.     return expr_as_conversion;
  1478.  
  1479.       error (msg, IDENTIFIER_POINTER (name));
  1480.       return error_mark_node;
  1481.     }
  1482.  
  1483.   if (! TYPE_HAS_CONSTRUCTOR (type))
  1484.     {
  1485.       if (expr_as_method || expr_as_fncall)
  1486.     goto return_function;
  1487.       if (expr_as_conversion)
  1488.     return expr_as_conversion;
  1489.  
  1490.       /* Look through this type until we find the
  1491.      base type which has a constructor.  */
  1492.       do
  1493.     {
  1494.       tree binfos = TYPE_BINFO_BASETYPES (type);
  1495.       int i, index = 0;
  1496.  
  1497.       while (binfos && TREE_VEC_LENGTH (binfos) == 1
  1498.          && ! TYPE_HAS_CONSTRUCTOR (type))
  1499.         {
  1500.           type = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
  1501.           binfos = TYPE_BINFO_BASETYPES (type);
  1502.         }
  1503.       if (TYPE_HAS_CONSTRUCTOR (type))
  1504.         break;
  1505.       /* Hack for MI.  */
  1506.       i = binfos ? TREE_VEC_LENGTH (binfos) : 0;
  1507.       if (i == 0) break;        
  1508.       while (--i > 0)
  1509.         {
  1510.           if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (TREE_VEC_ELT (binfos, i))))
  1511.         {
  1512.           if (index == 0)
  1513.             index = i;
  1514.           else
  1515.             {
  1516.               error ("multiple base classes with constructor, ambiguous");
  1517.               type = 0;
  1518.               break;
  1519.             }
  1520.         }
  1521.         }
  1522.       if (type == 0)
  1523.         break;
  1524.     } while (! TYPE_HAS_CONSTRUCTOR (type));
  1525.       if (type == 0)
  1526.     return error_mark_node;
  1527.     }
  1528.   name = TYPE_NAME (type);
  1529.   if (TREE_CODE (name) == TYPE_DECL)
  1530.     name = DECL_NAME (name);
  1531.  
  1532.   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 321);
  1533.  
  1534.   {
  1535.     int flags = LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN;
  1536.  
  1537.     if (parms && TREE_CHAIN (parms) == NULL_TREE)
  1538.       flags |= LOOKUP_NO_CONVERSION;
  1539.  
  1540.   try_again:
  1541.     expr_as_ctor = build_method_call (NULL_TREE, name, parms, NULL_TREE, flags);
  1542.  
  1543.     if (expr_as_ctor && expr_as_ctor != error_mark_node)
  1544.       {
  1545. #if 0
  1546.     /* mrs Mar 12, 1992 I claim that if it is a constructor, it is
  1547.        impossible to be an expr_as_method, without being a
  1548.        constructor call. */
  1549.     if (expr_as_method
  1550.         || (expr_as_fncall && expr_as_fncall != error_mark_node))
  1551. #else
  1552.     if (expr_as_fncall && expr_as_fncall != error_mark_node)
  1553. #endif
  1554.       {
  1555.         error ("ambiguity between constructor for `%s' and function call",
  1556.            IDENTIFIER_POINTER (name));
  1557.         return error_mark_node;
  1558.       }
  1559.     else if (expr_as_conversion && expr_as_conversion != error_mark_node)
  1560.       {
  1561.         /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
  1562.         error ("ambiguity between conversion to `%s' and constructor",
  1563.            IDENTIFIER_POINTER (name));
  1564.         return error_mark_node;
  1565.       }
  1566.  
  1567.     if (current_function_decl)
  1568.       return build_cplus_new (type, expr_as_ctor, 0);
  1569.  
  1570.     {
  1571.       register tree parm = TREE_OPERAND (expr_as_ctor, 1);
  1572.  
  1573.       /* Initializers for static variables and parameters have
  1574.          to handle doing the initialization and cleanup themselves.  */
  1575.       my_friendly_assert (TREE_CODE (expr_as_ctor) == CALL_EXPR, 322);
  1576. #if 0
  1577.       /* The following assertion fails in cases where we are initializing
  1578.          a static member variable of a particular instance of a template
  1579.          class with a call to a constructor of the given instance, as in:
  1580.  
  1581.         TMPL<int> object = TMPL<int>();
  1582.  
  1583.          Curiously, the assertion does not fail if we do the same thing
  1584.          for a static member of a non-template class, as in:
  1585.  
  1586.         T object = T();
  1587.  
  1588.          I can't see why we should care here whether or not the initializer
  1589.          expression involves a call to `new', so for the time being, it
  1590.          seems best to just avoid doing this assertion.  */
  1591.       my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)), 323);
  1592. #endif
  1593.       TREE_VALUE (parm) = NULL_TREE;
  1594.       expr_as_ctor = build_indirect_ref (expr_as_ctor, NULL_PTR);
  1595.       TREE_HAS_CONSTRUCTOR (expr_as_ctor) = 1;
  1596.     }
  1597.     return expr_as_ctor;
  1598.       }
  1599.  
  1600.     /* If it didn't work going through constructor, try type conversion.  */
  1601.     if (! (flags & LOOKUP_COMPLAIN))
  1602.       {
  1603.     if (expr_as_conversion)
  1604.       return expr_as_conversion;
  1605.     if (flags & LOOKUP_NO_CONVERSION)
  1606.       {
  1607.         flags = LOOKUP_NORMAL;
  1608.         goto try_again;
  1609.       }
  1610.       }
  1611.  
  1612.     if (expr_as_conversion)
  1613.       {
  1614.     if (expr_as_method || expr_as_fncall)
  1615.       {
  1616.         error ("ambiguity between conversion to `%s' and function call",
  1617.            IDENTIFIER_POINTER (name));
  1618.         return error_mark_node;
  1619.       }
  1620.     return expr_as_conversion;
  1621.       }
  1622.   return_function:
  1623.     if (expr_as_method)
  1624.       return build_method_call (current_class_decl, name, parms,
  1625.                 NULL_TREE, LOOKUP_NORMAL);
  1626.     if (expr_as_fncall)
  1627.       return expr_as_fncall == error_mark_node
  1628.     ? build_overload_call (name, parms, 1, (struct candidate *)0)
  1629.       : expr_as_fncall;
  1630.     error ("invalid functional cast");
  1631.     return error_mark_node;
  1632.   }
  1633. }
  1634.  
  1635. /* Return the character string for the name that encodes the
  1636.    enumeral value VALUE in the domain TYPE.  */
  1637. char *
  1638. enum_name_string (value, type)
  1639.      tree value;
  1640.      tree type;
  1641. {
  1642.   register tree values = TYPE_VALUES (type);
  1643.   register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value);
  1644.  
  1645.   my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324);
  1646.   while (values
  1647.      && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval)
  1648.     values = TREE_CHAIN (values);
  1649.   if (values == NULL_TREE)
  1650.     {
  1651.       char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type));
  1652.  
  1653.       /* Value must have been cast.  */
  1654.       sprintf (buf, "(enum %s)%d",
  1655.            TYPE_NAME_STRING (type), intval);
  1656.       return buf;
  1657.     }
  1658.   return IDENTIFIER_POINTER (TREE_PURPOSE (values));
  1659. }
  1660.  
  1661. /* Print out a language-specific error message for
  1662.    (Pascal) case or (C) switch statements.
  1663.    CODE tells what sort of message to print. 
  1664.    TYPE is the type of the switch index expression.
  1665.    NEW is the new value that we were trying to add.
  1666.    OLD is the old value that stopped us from adding it.  */
  1667. void
  1668. report_case_error (code, type, new_value, old_value)
  1669.      int code;
  1670.      tree type;
  1671.      tree new_value, old_value;
  1672. {
  1673.   if (code == 1)
  1674.     {
  1675.       if (new_value)
  1676.     error ("case label not within a switch statement");
  1677.       else
  1678.     error ("default label not within a switch statement");
  1679.     }
  1680.   else if (code == 2)
  1681.     {
  1682.       if (new_value == 0)
  1683.     {
  1684.       error ("multiple default labels in one switch");
  1685.       return;
  1686.     }
  1687.       if (TREE_CODE (new_value) == RANGE_EXPR)
  1688.     if (TREE_CODE (old_value) == RANGE_EXPR)
  1689.       {
  1690.         char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1691.         if (TREE_CODE (type) == ENUMERAL_TYPE)
  1692.           sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression",
  1693.                enum_name_string (TREE_OPERAND (new_value, 0), type),
  1694.                enum_name_string (TREE_OPERAND (new_value, 1), type),
  1695.                enum_name_string (TREE_OPERAND (old_value, 0), type),
  1696.                enum_name_string (TREE_OPERAND (old_value, 1), type));
  1697.         else
  1698.           sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression",
  1699.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
  1700.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
  1701.                TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
  1702.                TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)));
  1703.         error (buf);
  1704.       }
  1705.     else
  1706.       {
  1707.         char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1708.         if (TREE_CODE (type) == ENUMERAL_TYPE)
  1709.           sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
  1710.                enum_name_string (TREE_OPERAND (new_value, 0), type),
  1711.                enum_name_string (TREE_OPERAND (new_value, 1), type),
  1712.                enum_name_string (old_value, type));
  1713.         else
  1714.           sprintf (buf, "range [%d..%d] includes (%d) in case expression",
  1715.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)),
  1716.                TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)),
  1717.                TREE_INT_CST_LOW (old_value));
  1718.         error (buf);
  1719.       }
  1720.       else if (TREE_CODE (old_value) == RANGE_EXPR)
  1721.     {
  1722.       char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type)));
  1723.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1724.         sprintf (buf, "range [%s..%s] includes element `%s' in case expression",
  1725.              enum_name_string (TREE_OPERAND (old_value, 0), type),
  1726.              enum_name_string (TREE_OPERAND (old_value, 1), type),
  1727.              enum_name_string (new_value, type));
  1728.       else
  1729.         sprintf (buf, "range [%d..%d] includes (%d) in case expression",
  1730.              TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)),
  1731.              TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)),
  1732.              TREE_INT_CST_LOW (new_value));
  1733.       error (buf);
  1734.     }
  1735.       else
  1736.     {
  1737.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1738.         error ("duplicate label `%s' in switch statement",
  1739.            enum_name_string (new_value, type));
  1740.       else
  1741.         error ("duplicate label (%d) in switch statement",
  1742.            TREE_INT_CST_LOW (new_value));
  1743.     }
  1744.     }
  1745.   else if (code == 3)
  1746.     {
  1747.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1748.     warning ("case value out of range for enum %s",
  1749.          TYPE_NAME_STRING (type));
  1750.       else
  1751.     warning ("case value out of range");
  1752.     }
  1753.   else if (code == 4)
  1754.     {
  1755.       if (TREE_CODE (type) == ENUMERAL_TYPE)
  1756.     error ("range values `%s' and `%s' reversed",
  1757.            enum_name_string (new_value, type),
  1758.            enum_name_string (old_value, type));
  1759.       else
  1760.     error ("range values reversed");
  1761.     }
  1762. }
  1763.